/******************************************************************************
 * file  : console.c
 * creat : apleilx
 * data  : 2021/01/20
 *******************************************************************************/
#include "pincfg.h"
#include "bsp_api.h"
#include "os_obj.h"
#include "com_obj.h"
#include "dataproc.h"
#include "framework.h"
#include "simp_txt_analysis.h"
#include "password.h"
#include "iohandle.h"
#include "multi_malloc.h"

/*******************************************************************************
 * @brief  cmd back dat
 *******************************************************************************/
uint32_t cmd_back_dat[20];

/*******************************************************************************
 * @brief  cmd callback interface
 *******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char *txt_src, uint16_t len, int16_t cmd_num);

/*******************************************************************************
 * @brief  cmd index
 *******************************************************************************/
#define CMD_ATTR_ADMIN 1
#define CMD_ATTR_FACTORY (CMD_ATTR_ADMIN + 1)
#define CMD_ATTR_PASSWORD (CMD_ATTR_FACTORY + 1)
#define CMD_ATTR_CMD (CMD_ATTR_PASSWORD + 1)
#define CMD_ATTR_DEV_ID (CMD_ATTR_CMD + 1)
#define CMD_ATTR_DEV_NAME (CMD_ATTR_DEV_ID + 1)
#define CMD_ATTR_FAC_ID (CMD_ATTR_DEV_NAME + 1)
#define CMD_ATTR_FAC_NAME (CMD_ATTR_FAC_ID + 1)
#define CMD_ATTR_HIST_OUT (CMD_ATTR_FAC_NAME + 1)
#define CMD_ATTR_ALR_OUT (CMD_ATTR_HIST_OUT + 1)
#define CMD_ATTR_LOG_OUT (CMD_ATTR_ALR_OUT + 1)
#define CMD_ATTR_SIZE (CMD_ATTR_LOG_OUT + 1)

#define CMD_ATTR_END 127

#if CMD_ATTR_SIZE != 12
#error cfg cmd define error "at usb_file_cfg.c"
#endif

/*******************************************************************************
 * @brief  cmd define.
 * bit 15    : not display in info.txt
 * bit 14    : not display in any file
 * bit 8-11  : 0 无需权限， 1-管理权限， 2-工厂权限
 *******************************************************************************/
const txt_cmd_cfg_type cmd_list[] = {
	{0x4000 | CMD_ATTR_ADMIN, "LOGIN.ADMIN", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback, "# 管理员登录\n LOGIN.ADMIN=密码\n"},
	{0x4000 | CMD_ATTR_FACTORY, "LOGIN.FACTORY", TXT_CMD_TYPE_LONG, cmd_back_dat, 1, cmd_callback, "# 工厂登录\n LOGIN.FACTORY=密码\n"},
	{0x4100 | CMD_ATTR_PASSWORD, "PASSWORD", TXT_CMD_TYPE_STRING, cmd_back_dat, 16, cmd_callback, "# 管理员密码设置\n PASSWORD=密码\n"},
	{0x8000 | CMD_ATTR_CMD, "CMD", TXT_CMD_TYPE_NOP, cmd_back_dat, 1, cmd_callback, "# 杂项命令\n CMD=?\n"},
	{0x0000 | CMD_ATTR_DEV_ID, "DEVICE.ID", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback, "# 设备序列号：最大24个字符 \n DEVICE.ID=\n"},
	{0x0000 | CMD_ATTR_DEV_NAME, "DEVICE.NAME", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback, "# 设备名称：最大24个字符 \n DEVICE.NAME=\n"},
	{0x0000 | CMD_ATTR_FAC_ID, "FAC.ID", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback, "# 出厂设备ID：最大24个字符 \n FAC.ID=\n"},
	{0x0000 | CMD_ATTR_FAC_NAME, "FAC.NAME", TXT_CMD_TYPE_STRING, cmd_back_dat, 24, cmd_callback, "# 出厂设备名称：最大24个字符 \n FAC.NAME=\n"},
	{0x0000 | CMD_ATTR_HIST_OUT, "OUT.HIST", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback, "# 历史数据导出：导出指定时间段内的数据\n OUT.HIST=年 月 日 年 月 日\n"},
	{0x0000 | CMD_ATTR_ALR_OUT, "OUT.ALM", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback, "# 报警日志导出：导出指定时间段内的报警记录\n OUT.ALM=年 月 日 年 月 日\n"},
	{0x0000 | CMD_ATTR_LOG_OUT, "OUT.LOG", TXT_CMD_TYPE_SHORT, cmd_back_dat, 6, cmd_callback, "# 操作日志导出：导出指定时间段内的操作日志\n OUT.LOG=年 月 日 年 月 日\n"},
	
	{CMD_ATTR_END, "END", TXT_CMD_TYPE_END, cmd_back_dat, 1, cmd_callback},
};

/*******************************************************************************
 * @brief  cmd_callback.
 * \param[in] cmd_site 命令位置
 * \param[in] txt_src 文本原文
 * \param[in] len 数据长度
 * \param[in] cmd_num 数组模式cmd索引
 * \retval: none
 *******************************************************************************/
void cmd_callback(uint16_t cmd_site, const char *txt_src, uint16_t len, int16_t cmd_num)
{
	char *i8p;
	int16_t *i16p;
	int32_t *i32p;

	if (cmd_site > sizeof(cmd_list) / sizeof(txt_cmd_cfg_type))
		return;

	i8p = cmd_list[cmd_site].des;
	i16p = cmd_list[cmd_site].des;
	i32p = cmd_list[cmd_site].des;

	(void)i8p;
	(void)i16p;
	(void)i32p;

	switch (cmd_list[cmd_site].attr & 0xFF)
	{
	case CMD_ATTR_ADMIN:
	{
		dbg_puts("CMD_ADMIN\n");
		break;
	}
	case CMD_ATTR_FACTORY:
	{
		dbg_puts("CMD_FACTORY\n");
		break;
	}
	case CMD_ATTR_PASSWORD:
	{

		break;
	}
	case CMD_ATTR_CMD:
	{
		break;
	}
	case CMD_ATTR_DEV_ID:
	{
		break;
	}
	case CMD_ATTR_DEV_NAME:
	{
		break;
	}
	case CMD_ATTR_FAC_ID:
	{
		break;
	}
	case CMD_ATTR_FAC_NAME:
	{
		break;
	}
	default:
	{
		break;
	}
	}
}
